package cac.fenjian.resource.AoTask.MsTaskPackage.service;

import cac.fenjian.resource.AoTask.MsTaskPackage.dao.MsTaskCheckOperDao;
import cac.fenjian.resource.AoTask.MsTaskPackage.util.MsTaskCheckOperUtil;
import com.cac.dme.adapter.rest.msg.ResponseMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MsTaskCheckOperService {
    @Autowired
    private MsTaskCheckOperDao msTaskCheckOperDao;
    @Autowired
    private MsTaskCheckOperUtil msTaskCheckOperUtil;
    /**
     * 检查当前分拣台 是否执行任务
     * **/
    public HashMap checkTaskIsExe(String ip){
        return msTaskCheckOperDao.queryTaskIsOper(ip);
    }

    /**
     * 校验当前托盘处理的AO是否处理完成(是否需要叫下一个托盘)
     * */
    public ResponseMsg checkTPTaskIsFinal(String type,String indexTm){
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.setRetCode("200");
        /*if("package".equals(type)){
            responseMsg = msTaskCheckOperUtil.checkScanPackage(indexTm);
        }else if("hcx".equals(type)){//判断缓存箱以及工作包是否需要回库
            responseMsg = msTaskCheckOperUtil.checkSacnHcx(indexTm);
        }*/
        return responseMsg;
    }

    /**
     * @Param:indexTm 缓存箱，托盘条码
     * */
    public ResponseMsg insertConsoleOperLog(String indexTm){
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.setRetCode("200");
        responseMsg.setMessage("操作成功");
       /* if(indexTm==null||"".equals(indexTm)) {
            return responseMsg;
        }
        String ip = null;
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        if (request.getHeader("x-forwarded-for") == null) {
            ip = request.getRemoteAddr();
        } else {
            ip = request.getHeader("x-forwarded-for");
        }
        HashMap operData = new HashMap();
        HashMap curAo = null;
        List<HashMap> curAoInfos = null;
        if(indexTm.startsWith("1")){//缓存箱
            operData.put("palletType","hcx");
            curAoInfos = msTaskCheckOperDao.searchCurOperHcxAo(ip);
        }else if(indexTm.startsWith("2")){//托盘
            operData.put("palletType","package");
            curAoInfos = msTaskCheckOperDao.searchCurOperPagcakgeAo(ip);
        }
        if(curAoInfos.size()>0){
            curAo = curAoInfos.get(0);
        }else{
            return responseMsg;
        }
        operData.put("msGkLogId",curAo.get("msGkLogId"));
        operData.put("packageId",curAo.get("packageId"));
        operData.put("aoCode",curAo.get("aoCode"));
        operData.put("ip",ip);
        operData.put("palletCode",indexTm);
        msTaskCheckOperDao.insertMsConsoleOper(operData);*/
        return responseMsg;
    }

    /***
     * 更新分拣台操作记录
     * 缓存箱、托盘回库会调用
     * */
    public ResponseMsg updateMsConsoleOper(String palletType,String operStatus){
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.setRetCode("200");
        responseMsg.setMessage("操作成功");
      /*  String ip = null;
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        if (request.getHeader("x-forwarded-for") == null) {
            ip = request.getRemoteAddr();
        } else {
            ip = request.getHeader("x-forwarded-for");
        }
        String isNeedBack = "N";
        responseMsg.setErrorDesc("N");
        if("hcx".equals(palletType)){//判断当前托盘是否需要回库
            //获取当前处理的AO以及工作包信息
            List<HashMap> hcxAoInfos = msTaskCheckOperDao.searchCurOperHcxAo(ip);
            List<HashMap> packageAoInfos = msTaskCheckOperDao.searchCurOperPagcakgeAo(ip);
            if(hcxAoInfos.size()==0){//下一个操作的箱子没有，就应该将缓箱子回库
                //需要回箱子
                isNeedBack = "Y";
                responseMsg.setErrorDesc(isNeedBack);
                //更改操作记录表工作包回包标识
                msTaskCheckOperDao.updateMsConsoleOper(ip,"0","package","Y");
            }else if(!hcxAoInfos.get(0).get("packageId").equals(packageAoInfos.get(0).get("packageId"))){
                //需要回箱子
                isNeedBack = "Y";
                responseMsg.setErrorDesc(isNeedBack);
                //更改操作记录表工作包回包标识
                msTaskCheckOperDao.updateMsConsoleOper(ip,"0","package","Y");
            }
            msTaskCheckOperDao.updateMsConsoleOper(ip,operStatus,palletType,"N");
        }else{
            msTaskCheckOperDao.updateMsConsoleOper(ip,operStatus,"package","Y");
        }
        */
        return responseMsg;
    }
}
